<?php

// +----------------------------------------------------------------------
// | 私信
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\event\index;

use app\common\event\Base;
use app\model\ucenter\User as UserModel;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use think\facade\Db;

class Agent extends Base
{


    public static array $levels = [

        [
            'num'=>0,
            'deposit_rate'=>0.20,
            'profit_rate'=>0.03,
        ],
        [
            'num'=>31,
            'deposit_rate'=>0.3,
            'profit_rate'=>0.04,
        ],
        [
            'num'=>61,
            'deposit_rate'=>0.4,
            'profit_rate'=>0.05,
        ],
    ];
    /**
     * 云副业代理服务商政策
     * 新增有效合伙人0-30，押金奖励20%，收益奖励3%
     * 31-60押金奖励30%，收益奖励4%，
     * 60以上，押金奖励40，收益奖励5%
     */
    public function monthBill()
    {
        try {
//				Db::startTrans();

            $users = UserModel::where('level',10)->select();


            foreach ($users as $index => $user) {

                // 上月有效用户
                $user_count = UserModel::whereTime('effective_time','last month')->where('agent_id',$user['id'])->count();

                if ($user_count<30)
                {
                    $level = self::$levels[0];
                }elseif ($user_count<60)
                {
                    $level = self::$levels[1];
                }else{

                    $level = self::$levels[2];
                }
                // 保证金统计、
                $deposit =  UserMoneyLogModel::whereTime('create_time','last month')
                    ->where('agent_id',$user['id'])
                    ->where('type',93)
                    ->sum('money');
                // 收益统计、
                $fee =  UserMoneyLogModel::whereTime('create_time','last month')
                    ->where('agent_id',$user['id'])
                    ->where('type','in',[11,90])
                    ->sum('money');
            }

            dump($users->toArray());
//				Db::commit();
            $data['code'] = 0;
            $data['message'] = 'ok';
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $data['code'] = 2;
            $data['message'] = 'file:' . $e->getFile() . ',message:' . $e->getMessage() . 'li' . $e->getLine();
//				$data['message'] = $e->getMessage();
        }
        return $data;
    }

}
